#include <QVector>
#include <math.h>
#include "server.h"
#include "pagegenerator.h"
#include "filehtmldata.h"
#include "utils.h"


PageGenerator::PageGenerator(Server *s)
{
    this->server = s;
}


PageGenerator::~PageGenerator()
{

}





QString PageGenerator::genHtmlForFolder( const QString & dir)
{
    qWarning() << __FUNCTION__ << dir;

    QString html;

    /* берём дерево каталога. dir - полный путь */
    VirtualFsObject *vfso = server->getVirtualFs()->folder(dir);

    /* список файлов для генерируемой страницы */
    FileHTMLDataList sl;
    QString ranges;
    int sz;

    if(!vfso)
    {
        html = QObject::trUtf8("HTTP/1.0 404 Not Found\r\n"
                                  "Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"
                                  "<html><body><ul>\n");
        html += "Ups, page not found :(";
        goto end;
    }

    if(!vfso->size())
    {
        html = QObject::trUtf8("HTTP/1.0 200 OK\r\n"
                                  "Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"
                                  "<html><body><ul>\n");
        html += "Folder is empty";
        goto end;
    }

    html = QObject::trUtf8("HTTP/1.0 200 OK\r\n"
                              "Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"
                              "<html><body><ul>\n");


    for(int i=0; i<vfso->size(); ++i)
    {
        quint64 fsz = vfso->at(i)->isDir()? 0 : QFile(vfso->at(i)->realWay() + vfso->at(i)->realName()).size();
        qWarning() << "File size: " << fsz << vfso->at(i)->realWay() + vfso->at(i)->name();

        if(vfso->at(i)->isHidden())
            continue;

        double _sz = Utils::bytesToHigherDimensions(fsz, ranges);

        sl << FileHTMLData(vfso->at(i)->name(), _sz, vfso->at(i)->isDir(), ranges);
    }

    if(sl.isEmpty()) {
        html += "Folder is empty";
        goto end;
    }

    sl.sort();


    sz = sl.dirs_size();
    for(int i = 0; i < sz; ++i)
    {
        if(!sl.dirs_at(i)){
            qWarning() << "Dirs array out of range!" << i;
            continue;
        }

        html += QObject::trUtf8(
                    "<li><a href='%1%2/'>%2/</a></li>\n"
                    ).arg(dir).arg( sl.dirs_at(i)->name() );

    }

    sz = sl.files_size();
    for(int i = 0; i < sz; ++i)
    {
        if(!sl.files_at(i)){
            qWarning() << "Files array out of range!" << i;
            continue;
        }

        QString n = QString::number(sl.files_at(i)->size(), 'f', 2);
        if(n.endsWith(".00"))
            n.remove( n.length()-3, 3 );

        html += QObject::trUtf8(
                    "<li><a href='%1%2'>%2 (%3 %4)</a></li>\n"
                    ).arg(dir).arg(
                    sl.files_at(i)->name()).arg(n).arg(
                    sl.files_at(i)->range());
    }


end:
    html += QObject::trUtf8( "</ul></body></html>\n" );

    return html;
}




QString PageGenerator::genError(const QString & error_text )
{
    QString s = error_text;
    s = QObject::trUtf8("HTTP/1.0 200 Ok\r\n"
               "Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"
               "%1").arg(s.replace("\r\n", "<br>"));
    return s;
}
